import {observable, action} from 'mobx';
import {Form, ListPage} from 'weaver-mobile-page';
import {Toast,Tools} from "weaver-mobile";
import * as API from '../../api/cptApi';

const getLabel = Tools.getLabel;

export default class OperationsStore {
    @observable isLoading = true;
    @observable instockid = '';
    @observable isChecked = '';
    @observable form = new Form.Store();
    @observable listPageStore = new ListPage.Store({route:'/acceptanceDetail'});

    @action
    init = (id, opt) => {
        let params = {
            instockid: id
        };
        if (opt === 'batch') {//批量操作
            params = {
                ...params,
                viewtype: opt
            }
        }
        this.getInstockDetail(params);
        this.instockid = id;
    };
    @action
    getInstockDetail = (params) => {
        this.isLoading = true;
        API.getInstockDetail(params).then((res) => {
            this.isLoading= false;
            const {fieldinfo, datas, isChecked} = res;
            this.isChecked = isChecked;
            this.form = new Form.Store();
            this.form.initForm(fieldinfo, '/acceptanceDetail');
            this.listPageStore.setState({data: datas});
        });
    }

    @action
    getParams = () => {
        return this.form.formParams;
    };

    @action
    updateField = (key, value) => {
        this.form.updateField(key, value);
    };
    @action
    updateFields = (fields) => {
        this.form.updateFields(fields)
    };
    @action
    clearForm = () => {
        this.form.resetFields();
    };
    @action
    formChange = (field, value) => {
        // console.log("form事件触发：", field, toJS(value))
        if (field.conditionType === "BROWSER") {
            if (field.browserType === '4') {   //browserType 4:部门

            }

        }
    };
    @action
    applyDetails = (history) => {
        this.doInstockCheck(history);
    };
    @action
    doInstockCheck = (history) => {
        const formParams = this.getParams();
        const {cptdept_to} = formParams;
        
        let re = this.form.doValidate();
        if(!re.pass){
            return;
        }

        const params = {
            instockid: this.instockid,
            cptdept_to: cptdept_to
        };
        this.isLoading=true;
        API.doInstockCheck(params).then((res) => {
            this.isLoading=false;
            const {flag} = res;
            if (flag) {
                this.isLoading=true;
                API.doInstockCapital(params).then((res) => {
                    this.isLoading=false;
                    const {flag} = res;
                    if (flag) {
                        Toast.success(getLabel(389844,'验收成功'), 2);
                        history.replace('/acceptance/0')
                    } else {
                        Toast.fail(getLabel(389845,'验收失败'), 2);
                    }
                })
            } else {
                Toast.fail(res.msg, 2);
            }
        });

    }
}
